﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Helpers;
using myMvcBlog.Models;

namespace myMvcBlog.Controllers
{
    public class CommentController : BaseController
    {
        //
        // GET: /Comment/

        [ValidateInput(false)]
        public void Add(Comment comment)
        {
            long postId = 0;
            if (!long.TryParse(Request.Form["PostID"], out postId))
            {
                return;
            }

            comment.PostID = postId;
            comment.CommentTime = DateTime.Now;
            comment.IP = Request.UserHostAddress;
            comment.Save();            

            Post post = Post.SingleOrDefault(p => p.ID == postId);
            string postUrl = Url.RouteUrl("ViewPost", new { nicename = post.UrlName });
            Response.Redirect(postUrl + "#comment" + comment.ID);
        }
    }
}
